package edu.uoc.tfc.web.tags;


import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;



public class TextFieldTag extends AbstractFormFieldTag 
{



    private static final long serialVersionUID = 1L;   

	@Override
    protected void doStartFormField(JspWriter out) throws JspException
	{
		try
		{
			if (isWritable())
			{
				if (NumberUtils.toInt(getRows(), -1) > 0)
				{
					write(out,"<textarea class=\"" + getClassMode() + "\" rows=\"" + getRows() + "\" name=\"" + getName() + "\" id=\"" + getId() + "\">" + getValue() + "</textarea>");
				}
				else
				{
		            write(out,"<input class=\"" + getClassMode() + "\" type=\"text\" name=\"" + getName() + "\" id=\"" + getId() + "\" value=\"" + getValue() + "\" />");
				}
			}
			else
			{
				writeNotEditableField(out);
			}
		}
		catch (Exception ex)
		{
			String message = "Error generando Campo de texto: " + this.getProperty();
			throw new JspException(message);
		}
	}



	@Override
    protected void doEndFormField(JspWriter out) throws JspException
    {
		// No hacemos nada
    }



	protected void writeNotEditableField(JspWriter out)
	throws Exception
	{
		String showedValue;
		if (StringUtils.isBlank(getValue()))
		{
			showedValue = "&nbsp;";
		}
		else
		{
			showedValue = getValue();
		}
		write(out,"<span class=\"" + getClassMode() + " uneditable-textarea\">" + showedValue + "</span>");
		write(out,"<input type=\"hidden\" name=\"" + getName() + "\" id=\"" + getId() + "\" value=\"" + getValue() + "\" />");
    }



}
